/*
 * 文件名： ScanCaptureActivity.java
 * 描    述：扫描界面
 * 时    间：2015-3-25 下午5:47:21
 * 版    本：V1.0
 */
package com.miguo.activity;

import android.os.Bundle;
import android.text.TextUtils;

import com.miguo.framework.scan.ScanCapture;
import com.miguo.framework.scan.decode.CaptureActivityHandler;
import com.miguo.framework.utils.Toast;
import com.miguo.ui.R;
import com.miguo.utils.ScanUtils;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class ScanCaptureActivity extends ScanCapture implements
        ScanCapture.OnScanResultListener {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        setScanResultListener(this);
        super.onCreate(savedInstanceState);
    }

    @Override
    protected int getStatusBarColorResId() {
        return R.color.black;
    }

    @Override
    public void onScanResult(String result, CaptureActivityHandler handler) {
        if (!TextUtils.isEmpty(result)) {
            String http_regexp = "^(http://|https://|)([A-Za-z0-9-_.]+.|)(meguo.com|miguo.cn|miguo.com.cn|juduoduo.com)(/.*?|)";
            Pattern pattern = Pattern.compile(http_regexp);
            Matcher matcher = pattern.matcher(result);
            if (matcher.find() == true) {
                ScanUtils.getInstance(ScanCaptureActivity.this).getData(result,
                        handler);
            } else {
                Toast.show(mContext, "亲~扫描仅支持米果特卖二维码啦~!");
                // 连续扫描，不发送此消息扫描一次结束后就不能再次扫描
                handler.sendEmptyMessage(R.id.restart_preview);
            }
        } else {
            Toast.show(mContext, "亲~扫描的二维码有误啦~!");
            // 连续扫描，不发送此消息扫描一次结束后就不能再次扫描
            handler.sendEmptyMessage(R.id.restart_preview);
        }

    }

    @Override
    public void onScanResultNull() {
        Toast.show(mContext, "亲~扫描的二维码有误啦~!");
    }

}